/* 
 *	Copyright (C) 2005 Team MediaPortal
 *	http://www.team-mediaportal.com
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

using System;
using System.Drawing;
using System.IO;
using System.Collections;
using System.ComponentModel;
//using System.Windows.Forms;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Threading;
using MediaPortal.Util;
using MediaPortal.Dialogs;
using MediaPortal.Player;
using MediaPortal.Playlists;
using MediaPortal.TagReader;
using MediaPortal.GUI.Library;

namespace HDTrailerPlugin
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    public class GUIQTTrailers : GUIWindow
    {
        #region SkinControlAttributes
        [SkinControlAttribute(4)]
        protected GUIListControl listview = null;
        [SkinControlAttribute(24)]
        protected GUIImage poster = null;
        [SkinControlAttribute(50)]
        protected GUILabelControl label0 = null;
        [SkinControlAttribute(51)]
        protected GUIFadeLabel label1 = null;
        [SkinControlAttribute(52)]
        protected GUILabelControl label2 = null; //runtime
        [SkinControlAttribute(53)]
        protected GUILabelControl label3 = null;
        [SkinControlAttribute(54)]
        protected GUILabelControl label4 = null;
        [SkinControlAttribute(55)]
        protected GUILabelControl label5 = null;
        [SkinControlAttribute(56)]
        protected GUILabelControl label6 = null;
        [SkinControlAttribute(57)]
        protected GUITextScrollUpControl castarea = null;
        [SkinControlAttribute(58)]
        protected GUILabelControl label8 = null;
        [SkinControlAttribute(59)]
        protected GUITextScrollUpControl plotarea = null;
        //[SkinControlAttribute(60)]			protected GUIImageList labelrating =null;
        #endregion
        #region Variables

        public static bool _workerCompleted = true;

        //public QTPlayer _player;
        int Prev_SelectedItem = 0;	// remember listview selections
        int[] SelectedItem = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

        //private QTPlayer qtctrl = new QTPlayer();

        public static string TempHTML;	// GetWeb string
        public static string MMSUrl;
        string backgroundposter = null;

        string[] LMovieUrl = new string[200]; // strings for letterbutton movies
        string[] LMovieName = new string[200];

        DateTime RefreshDaily = DateTime.Now.Date; //for the daily update, if HTPC is on 24h it will refresh strings every day.

        bool mainview = false;


        public static string ptitle;			// Used by SetGUIProperties, GetGUIProperties
        public static string pgenre;			// Before MP start playing a movie fullscreen
        public static string pruntime;		// it wil save the #tags info in these string
        public static string preleasedate;    // and will load them back after playing the 
        public static string pplot;			// movie.
        public static string pcast;
        public static double prating;

        string[] MainListMenu = new String[34];  //Decause the number of menu item is dynamic.


        // BackGroundworker
        string _downloadedText = string.Empty;

        #endregion
        #region Override functions

        public override int GetID
        {
            get
            {
                return 6000;
            }
            set
            {
                base.GetID = value;
            }
        }
        public override bool Init()
        {
            /*
            _player = new QTPlayer();
            GUIGraphicsContext.form.Controls.Add(_player);
            _player.Visible = false;
            _player.Enabled = false;
             * */
            return Load(GUIGraphicsContext.Skin + @"\myHDtrailers.xml");
        }
        protected override void OnClicked(int controlId, GUIControl control, MediaPortal.GUI.Library.Action.ActionType actionType)  // For when a button is pressed
        {
            if (control == listview)
            {
                GUIMessage msg = new GUIMessage(GUIMessage.MessageType.GUI_MSG_ITEM_SELECTED, GetID, 0, controlId, 0, 0, null);
                OnMessage(msg);
                int itemIndex = (int)msg.Param1;
                if (actionType == Action.ActionType.ACTION_SELECT_ITEM)
                {
                    OnClick(itemIndex);
                }
            }
            base.OnClicked(controlId, control, actionType);
        }
        public override void OnAction(Action action)
        {
            base.OnAction(action);
        }
        protected override void OnPageDestroy(int newWindowId)
        {
            if (AppleTrailers.tview == true || AppleTrailers.cview == true || AppleTrailers.mview == true)
            {
                Prev_SelectedItem = listview.SelectedListItemIndex;
            }
            base.OnPageDestroy(newWindowId);
        }
        protected override void OnPageLoad()
        {
            base.OnPageLoad();
            LoadSettings();
            if (AppleTrailers.tview == true)
            {

                listview.SelectedListItemIndex = Prev_SelectedItem;
                listview.Focus = true;
            }
            else
            {
                ShowMainListView();
                listview.Focus = true;
            }
        }
        protected override void OnPreviousWindow()
        {
            base.OnPreviousWindow();
        }

        #endregion
        #region Button/Click functions

        private void OnClick(int itemindex) // // When something is pressed in the listview
        {

            // Trailerview
            if (AppleTrailers.tview == true)
            {
                if (itemindex == 0)
                {
                    AppleTrailers.tview = false;
                    ShowMainListView();
                    //ShowListView(AppleTrailers.trailers, true);
                    listview.SelectedListItemIndex = SelectedItem[2];
                }
                else
                {
                    Prev_SelectedItem = listview.SelectedListItemIndex;
                    ITMSClient.TrailerInfo trailer = AppleTrailers.ProcessTrailer(AppleTrailers.trailers[itemindex - 1]);
                    /*
                    if(trailer.Equals(null)==false){

                        Log.Info("trailer is not null");
                        if (trailer.TrailerStreamCollection != null)
                        {
                            Log.Info("trailer stream collection is not null");
                            if (trailer.TrailerStreamCollection.Equals(null) == false)
                            {
                                Log.Info("trailer stream [0] is not null");
                            }
                            if (trailer.TrailerStreamCollection[0].Movie != null)
                            {
                                Log.Info("trailer stream [0].Movie is not null");
                            }
                        }

                    }
                     */
                    Log.Info("TrailerURL:" + trailer.TrailerStreamCollection[0].Uri.ToString());
                    PlayTrailer(trailer.TrailerStreamCollection[0].Uri.ToString());
                }
            }
            // Main selection view
            else if (mainview == true)
            {
                mainview = false;
                BackgroundWorker worker = new BackgroundWorker();
                worker.DoWork += new DoWorkEventHandler(AppleTrailers.GetAppleTrailersRSS);
                AppleTrailers._workerCompleted = false;
                worker.RunWorkerAsync(listview[itemindex].Path);
                using (WaitCursor cursor = new WaitCursor())
                {
                    while (AppleTrailers._workerCompleted == false)
                        GUIWindowManager.Process();
                }
                //AppleTrailers.GetAppleTrailersRSS(listview[itemindex].Path);
                ShowListView(AppleTrailers.trailers, listview[itemindex].Label);
            }
        }
        #endregion
        void ShowMainListView()
        {
            mainview = true;
            AppleTrailers.allview = false;
            AppleTrailers.RSSView = false;
            poster.SetFileName(GUIGraphicsContext.Skin + @"\media\" + backgroundposter);
            GUIPropertyManager.SetProperty("#title", GUILocalizeStrings.Get(5902));

            string[,] MainListOptions = new string[37,2];
            MainListOptions[0,0] = "New Movie Trailers";
            MainListOptions[0, 1] = @"http://www.apple.com/moviesxml/h/view1_all.xml";
            MainListMenu[0] = MainListOptions[0, 0];
            MainListOptions[1,0] = "Action and Adventure";
            MainListOptions[1, 1] = @"http://movies.apple.com/moviesxml/g/action_and_adventure_1.xml";
            MainListMenu[1] = MainListOptions[1, 0];
            MainListOptions[2,0] = "Comedy";
            MainListOptions[2, 1] = @"http://movies.apple.com/moviesxml/g/comedy_1.xml";
            MainListMenu[2] = MainListOptions[2, 0];
            MainListOptions[3, 0] = "Documentary";
            MainListOptions[3, 1] = @"http://movies.apple.com/moviesxml/g/documentary_1.xml";
            MainListMenu[3] = MainListOptions[3, 0];
            MainListOptions[4, 0] = "Drama";
            MainListOptions[4, 1] = @"http://movies.apple.com/moviesxml/g/drama_1.xml";
            MainListMenu[4] = MainListOptions[4, 0];
            MainListOptions[5, 0] = "Family";
            MainListOptions[5, 1] = @"http://movies.apple.com/moviesxml/g/family_1.xml";
            MainListMenu[5] = MainListOptions[5, 0];
            MainListOptions[6, 0] = "Foreign";
            MainListOptions[6, 1] = @"http://movies.apple.com/moviesxml/g/fantasy_1.xml";
            MainListMenu[6] = MainListOptions[6, 0];
            MainListOptions[7, 0] = "Horror";
            MainListOptions[7, 1] = @"http://movies.apple.com/moviesxml/g/horror_1.xml";
            MainListMenu[7] = MainListOptions[7, 0];
            MainListOptions[8, 0] = "Musical";
            MainListOptions[8, 1] = @"http://movies.apple.com/moviesxml/g/musical_1.xml";
            MainListMenu[8] = MainListOptions[8, 0];
            MainListOptions[9, 0] = "Romance";
            MainListOptions[9, 1] = @"http://movies.apple.com/moviesxml/g/romance_1.xml";
            MainListMenu[9] = MainListOptions[9, 0];
            MainListOptions[10, 0] = "Sience Fiction";
            MainListOptions[10, 1] = @"http://movies.apple.com/moviesxml/g/science_fiction_1.xml";
            MainListMenu[10] = MainListOptions[10, 0];
            MainListOptions[11, 0] = "Thriller";
            MainListOptions[11, 1] = @"http://movies.apple.com/moviesxml/g/thriller_1.xml";
            MainListMenu[11] = MainListOptions[11, 0];
            MainListOptions[12, 1] = @"http://movies.apple.com/moviesxml/s/fox/index_1.xml"; 
MainListOptions[12, 0] = "20th Century Fox";
MainListMenu[12] = MainListOptions[12, 0];
MainListOptions[13, 1] = @"http://movies.apple.com/moviesxml/s/dreamworks/index_1.xml";
MainListOptions[13, 0] = "DreamWorks S.K.G";
MainListMenu[13] = MainListOptions[13, 0];
MainListOptions[14, 1] = @"http://movies.apple.com/moviesxml/s/focus_features/index_1.xml";
MainListOptions[14, 0] = "Focus Features / Rogue Pictures";
MainListMenu[14] = MainListOptions[14, 0];
MainListOptions[15, 1] = @"http://movies.apple.com/moviesxml/s/fox_searchlight/index_1.xml"; 
MainListOptions[15, 0] = "Fox Searchlight Pictures";
MainListMenu[15] = MainListOptions[15, 0];
MainListOptions[16, 1] = @"http://movies.apple.com/moviesxml/s/independent/index_1.xml";
MainListOptions[16, 0] = "Independent";
MainListMenu[16] = MainListOptions[16, 0];
MainListOptions[17, 1] = @"http://movies.apple.com/moviesxml/s/lions_gate/index_1.xml";
MainListOptions[17, 0] = "Lionsgate";
MainListMenu[17] = MainListOptions[17, 0];
MainListOptions[18, 1] = @"http://movies.apple.com/moviesxml/s/mgm/index_1.xml";
MainListOptions[18, 0] = "MGM Studios";
MainListMenu[18] = MainListOptions[18, 0];
MainListOptions[19, 1] = @"http://movies.apple.com/moviesxml/s/magnolia/index_1.xml";
MainListOptions[19, 0] = "Magnolia Pictures";
MainListMenu[19] = MainListOptions[19, 0];
MainListOptions[20, 1] = @"http://movies.apple.com/moviesxml/s/miramax/index_1.xml";
MainListOptions[20, 0] = "Miramax Films";
MainListMenu[20] = MainListOptions[20, 0];
MainListOptions[21, 1] = @"http://movies.apple.com/moviesxml/s/newline/index_1.xml";
MainListOptions[21, 0] = "New Line Cinema";
MainListMenu[21] = MainListOptions[21, 0];
MainListOptions[22, 1] = @"http://movies.apple.com/moviesxml/s/paramount/index_1.xml";
MainListOptions[22, 0] = "Paramount Pictures";
MainListMenu[22] = MainListOptions[22, 0];
MainListOptions[23, 1] = @"http://movies.apple.com/moviesxml/s/paramount_vantage/index_1.xml";
MainListOptions[23, 0] = "Paramount Vantage";
MainListMenu[23] = MainListOptions[23, 0];
MainListOptions[24, 1] = @"http://movies.apple.com/moviesxml/s/picturehouse/index_1.xml";
MainListOptions[24, 0] = "Picturehouse";
MainListMenu[24] = MainListOptions[24, 0];
MainListOptions[25, 1] = @"http://movies.apple.com/moviesxml/s/rogue_pictures/index_1.xml";
MainListOptions[25, 0] = "Rogue Pictures";
MainListMenu[25] = MainListOptions[25, 0];
MainListOptions[26, 1] = @"http://movies.apple.com/moviesxml/s/sony_pictures/index_1.xml";
MainListOptions[26, 0] = "Sony Pictures";
MainListMenu[26] = MainListOptions[26, 0];
MainListOptions[27, 1] = @"http://movies.apple.com/moviesxml/s/sony/index_1.xml";
MainListOptions[27, 0] = "Sony Pictures Classics";
MainListMenu[27] = MainListOptions[27, 0];
MainListOptions[28, 1] = @"http://movies.apple.com/moviesxml/s/touchstone/index_1.xml";
MainListOptions[28, 0] = "Touchstone Pictures";
MainListMenu[28] = MainListOptions[28, 0];
MainListOptions[29, 1] = @"http://movies.apple.com/moviesxml/s/universal/index_1.xml";
MainListOptions[29, 0] = "Universal Pictures";
MainListMenu[29] = MainListOptions[29, 0];
MainListOptions[30, 1] = @"http://movies.apple.com/moviesxml/s/disney/index_1.xml";
MainListOptions[30, 0] = "Walt Disney Pictures";
MainListMenu[30] = MainListOptions[30, 0];
MainListOptions[31, 1] = @"http://movies.apple.com/moviesxml/s/wb/index_1.xml";
MainListOptions[31, 0] = "Warner Bros. Pictures";
MainListMenu[31] = MainListOptions[31, 0];
MainListOptions[32, 1] = @"http://movies.apple.com/moviesxml/s/warner_independent_pictures/index_1.xml";
MainListOptions[32, 0] = "Warner Independent Pictures";
MainListMenu[32] = MainListOptions[32, 0];
MainListOptions[33, 1] = @"http://movies.apple.com/moviesxml/s/weinstein/index_1.xml";
MainListOptions[33, 0] = "Weinstein Company";
MainListMenu[33] = MainListOptions[33, 0];

            string language = "";
            using (MediaPortal.Profile.Settings xmlreader = new MediaPortal.Profile.Settings("MediaPortal.xml"))
            {
                language = xmlreader.GetValue("skin", "language");
            }
            int iNextItem = 10;

            listview.Clear();
            int i = 0;
            while (MainListOptions[i,0] != null)
            {
                GUIListItem item = new GUIListItem();
                item.IsFolder = true;
                MediaPortal.Util.Utils.SetDefaultIcons(item);
                item.Label = MainListOptions[i,0];
                item.Path = MainListOptions[i, 1];
                listview.Add(item);
                i++;
            }
        }

        /// <summary>
        /// Called when [message].
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        public override bool OnMessage(GUIMessage message)
        {

            //MediaPortal.GUI.Library.Log.Write("ONMessageeeee" + message.Message);
            return base.OnMessage(message);
        }
        public void PlayTrailer(string url)
        {
            /*
            if (!Utils.IsLiveRadio(url))
            {
                GUIMessage msgRadio = new GUIMessage(GUIMessage.MessageType.GUI_MSG_RECORDER_STOP_RADIO, 0, 0, 0, 0, 0, null);
                GUIWindowManager.SendMessage(msgRadio);
            }

            if (!Utils.IsLiveTv(url) && !Utils.IsLiveRadio(url))
            {
                //file is not a live tv file
                //so tell recorder to stop timeshifting live-tv
                Log.Write("player: file is not live tv, so stop timeshifting:{0}", url);
                GUIMessage msgTv = new GUIMessage(GUIMessage.MessageType.GUI_MSG_RECORDER_STOP_TIMESHIFT, 0, 0, 0, 0, 0, null);
                GUIWindowManager.SendMessage(msgTv);
            }

            _player.Visible = true;
            _player.Enabled = true;
            //GUIGraphicsContext.form.Focus();
            _player.Focus();
             */
            g_Player.Play(url);
            GUIGraphicsContext.IsFullScreenVideo = true;
            GUIWindowManager.ActivateWindow((int)GUIWindow.Window.WINDOW_FULLSCREEN_VIDEO);
            //GUIGraphicsContext.IsFullScreenVideo = true;
            //GUIWindowManager.ActivateWindow((int)GUIWindow.Window.WINDOW_FULLSCREEN_VIDEO);
            /*
                        try
                        {
                            _player.OpenMovie(url);
                            _player.axQTControl1.AutoPlay = "true";
                        }
                        catch (Exception ex)
                        {
                            Log.Write(ex.Message);
                        }
                        */
        }

        void LoadSettings()
        {
            if (AppleTrailers.tview == true)
            {
                AppleTrailers.tview = true;
                ShowListView(AppleTrailers.trailers, ptitle, true);
            }

        }
        public void ShowListView(List<ITMSClient.TrailerInfo> _TrailerName, bool show_poster)
        {
            if (show_poster == false)
                poster.SetFileName(GUIGraphicsContext.Skin + @"\media\" + backgroundposter);

            listview.Clear();
            GUIListItem item1 = new GUIListItem();
            item1.Label = "..";
            item1.IsFolder = true;
            MediaPortal.Util.Utils.SetDefaultIcons(item1);
            listview.Add(item1);

            foreach (ITMSClient.TrailerInfo trailerItem in _TrailerName)
            {
                GUIListItem item = new GUIListItem();
                item.IsFolder = true;
                MediaPortal.Util.Utils.SetDefaultIcons(item);
                item.Label = trailerItem.Title + " " + trailerItem.Type;
                listview.Add(item);
            }
        }
        public void ShowListView(List<ITMSClient.TrailerInfo> _TrailerName, int _titlenumber)
        {
            poster.SetFileName(GUIGraphicsContext.Skin + @"\media\" + backgroundposter);
            GUIPropertyManager.SetProperty("#title", GUILocalizeStrings.Get(_titlenumber));

            listview.Clear();
            GUIListItem item1 = new GUIListItem();
            item1.Label = "..";
            item1.IsFolder = true;
            MediaPortal.Util.Utils.SetDefaultIcons(item1);
            listview.Add(item1);

            foreach (ITMSClient.TrailerInfo trailerItem in _TrailerName)
            {
                GUIListItem item = new GUIListItem();
                item.IsFolder = true;
                MediaPortal.Util.Utils.SetDefaultIcons(item);
                item.Label = trailerItem.Title + " " + trailerItem.Type;
                listview.Add(item);
            }
        }
        public void ShowListView(List<ITMSClient.TrailerInfo> _TrailerName, string _titlename)
        {
            poster.SetFileName(GUIGraphicsContext.Skin + @"\media\" + backgroundposter);
            GUIPropertyManager.SetProperty("#title", _titlename);

            listview.Clear();
            GUIListItem item1 = new GUIListItem();
            item1.Label = "..";
            item1.IsFolder = true;
            MediaPortal.Util.Utils.SetDefaultIcons(item1);
            listview.Add(item1);

            foreach (ITMSClient.TrailerInfo trailerItem in _TrailerName)
            {
                GUIListItem item = new GUIListItem();
                item.IsFolder = true;
                MediaPortal.Util.Utils.SetDefaultIcons(item);
                item.Label = trailerItem.Title + " " + trailerItem.Type;
                listview.Add(item);
            }

        }
        public void ShowListView(List<ITMSClient.TrailerInfo> _TrailerName, string _titlename, bool show_poster)
        {
            if (show_poster == false)
                poster.SetFileName(GUIGraphicsContext.Skin + @"\media\" + backgroundposter);

            GUIPropertyManager.SetProperty("#title", _titlename);

            listview.Clear();
            GUIListItem item1 = new GUIListItem();
            item1.Label = "..";
            item1.IsFolder = true;
            MediaPortal.Util.Utils.SetDefaultIcons(item1);
            listview.Add(item1);

            foreach (ITMSClient.TrailerInfo trailerItem in _TrailerName)
            {
                GUIListItem item = new GUIListItem();
                item.IsFolder = true;
                MediaPortal.Util.Utils.SetDefaultIcons(item);
                item.Label = trailerItem.Title + " " + trailerItem.Type;
                item.IconImage = "defaultVideo.png";
                listview.Add(item);
            }

        }
        public void ShowListView(List<ITMSClient.TrailerInfo> _TrailerName, List<ITMSClient.TrailerStreamCollection> _TralerUrl, int _titlenumber)
        {
            poster.SetFileName(GUIGraphicsContext.Skin + @"\media\" + backgroundposter);
            GUIPropertyManager.SetProperty("#title", GUILocalizeStrings.Get(_titlenumber));

            listview.Clear();
            if (_TralerUrl.Count == 0)
            {
                GUIListItem item = new GUIListItem();
                item.Label = ".. - " + GUILocalizeStrings.Get(5916);
                item.IsFolder = true;
                item.IconImage = "defaultFolderBack.png";
                listview.Add(item);
                return;
            }
            else
            {
                GUIListItem item1 = new GUIListItem();
                item1.Label = "..";
                item1.IsFolder = true;
                MediaPortal.Util.Utils.SetDefaultIcons(item1);
                listview.Add(item1);

                foreach (ITMSClient.TrailerInfo trailerItem in _TrailerName)
                {
                    GUIListItem item = new GUIListItem();
                    item.IsFolder = true;
                    MediaPortal.Util.Utils.SetDefaultIcons(item);
                    item.Label = trailerItem.Title + " " + trailerItem.Type;
                    item.IconImage = "defaultVideo.png";
                    listview.Add(item);
                }
            }
        }
        public void ShowPoster(string downloadurl, string moviename)
        {
            if (downloadurl == null | downloadurl == string.Empty)
                return;
            else
            {
                poster.SetFileName(downloadurl);
            }
        }
        public void ShowMovieInfo(string moviename, string posterurl)
        {
            ShowPoster(posterurl, moviename);
        }

    }
}


